// ====================================================================================================================
// \Library\PackageCache\com.unity.render-pipelines.universal@14.0.9\ShaderLibrary\2D\Include\InputData2D.hlsl
// ====================================================================================================================

#ifndef COMBINED_SHAPE_LIGHT_PASS
#define COMBINED_SHAPE_LIGHT_PASS

#ifndef SURFACE_DATA_2D_INCLUDED
#define SURFACE_DATA_2D_INCLUDED

struct SurfaceData2D
{
    half3 albedo;
    half alpha;
    half4 mask;
    half3 normalTS;
};

void InitializeSurfaceData(half3 albedo, half alpha, half4 mask, half3 normalTS, out SurfaceData2D surfaceData)
{
    surfaceData = (SurfaceData2D)0;

    surfaceData.albedo = albedo;
    surfaceData.alpha = alpha;
    surfaceData.mask = mask;
    surfaceData.normalTS = normalTS;
}

void InitializeSurfaceData(half3 albedo, half alpha, half4 mask, out SurfaceData2D surfaceData)
{
    const half3 normalTS = half3(0, 0, 1);

    InitializeSurfaceData(albedo, alpha, mask, normalTS, surfaceData);
}

void InitializeSurfaceData(half3 albedo, half alpha, out SurfaceData2D surfaceData)
{
    InitializeSurfaceData(albedo, alpha, 1, surfaceData);
}

#endif


// ====================================================================================================================
// \Library\PackageCache\com.unity.render-pipelines.universal@14.0.9\ShaderLibrary\Debug\Debugging2D.hlsl
// ====================================================================================================================


#ifndef UNIVERSAL_DEBUGGING2D_INCLUDED
#define UNIVERSAL_DEBUGGING2D_INCLUDED

#include "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/InputData2D.hlsl"
#include "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/SurfaceData2D.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Debug/DebuggingCommon.hlsl"

#if defined(DEBUG_DISPLAY)

#define SETUP_DEBUG_TEXTURE_DATA_2D(inputData, positionWS, texture)    SetupDebugDataTexture(inputData, positionWS, texture##_TexelSize, texture##_MipInfo, GetMipCount(TEXTURE2D_ARGS(texture, sampler##texture)))
#define SETUP_DEBUG_DATA_2D(inputData, positionWS)                     SetupDebugData(inputData, positionWS)

void SetupDebugData(inout InputData2D inputData, float3 positionWS)
{
    inputData.positionWS = positionWS;
}

void SetupDebugDataTexture(inout InputData2D inputData, float3 positionWS, float4 texelSize, float4 mipInfo, uint mipCount)
{
    SetupDebugData(inputData, positionWS);

    inputData.texelSize = texelSize;
    inputData.mipInfo = mipInfo;
    inputData.mipCount = mipCount;
}

bool CalculateDebugColorMaterialSettings(in SurfaceData2D surfaceData, in InputData2D inputData, inout half4 debugColor)
{
    switch(_DebugMaterialMode)
    {
        case DEBUGMATERIALMODE_NONE:
        {
            return false;
        }

        case DEBUGMATERIALMODE_ALBEDO:
        {
            debugColor = half4(surfaceData.albedo, 1);
            return true;
        }

        case DEBUGMATERIALMODE_ALPHA:
        {
            debugColor = half4(surfaceData.alpha.rrr, 1);
            return true;
        }

        case DEBUGMATERIALMODE_SPRITE_MASK:
        {
            debugColor = surfaceData.mask;
            return true;
        }

        case DEBUGMATERIALMODE_NORMAL_TANGENT_SPACE:
        case DEBUGMATERIALMODE_NORMAL_WORLD_SPACE:
        {
            debugColor = half4(surfaceData.normalTS, 1);
            return true;
        }

        default:
        {
            return TryGetDebugColorInvalidMode(debugColor);
        }
    }
}

bool CalculateDebugColorForRenderingSettings(in SurfaceData2D surfaceData, in InputData2D inputData, inout half4 debugColor)
{
    if (CalculateColorForDebugSceneOverride(debugColor))
    {
        return true;
    }
    return false;
}

bool CalculateDebugColorLightingSettings(inout SurfaceData2D surfaceData, inout InputData2D inputData, inout half4 debugColor)
{
    switch(_DebugLightingMode)
    {
        case DEBUGLIGHTINGMODE_NONE:
        {
            return false;
        }

        case DEBUGLIGHTINGMODE_LIGHTING_WITHOUT_NORMAL_MAPS:
        case DEBUGLIGHTINGMODE_LIGHTING_WITH_NORMAL_MAPS:
        {
            surfaceData.albedo = 1;
            return false;
        }

        default:
        {
            return TryGetDebugColorInvalidMode(debugColor);
        }
    }       // End of switch.
}

bool CalculateDebugColorValidationSettings(in SurfaceData2D surfaceData, in InputData2D inputData, inout half4 debugColor)
{
    switch(_DebugMaterialValidationMode)
    {
        case DEBUGMATERIALVALIDATIONMODE_NONE:
            return false;

        case DEBUGMATERIALVALIDATIONMODE_ALBEDO:
            return CalculateValidationAlbedo(surfaceData.albedo, debugColor);

        default:
            return TryGetDebugColorInvalidMode(debugColor);
    }
}

bool CanDebugOverrideOutputColor(inout SurfaceData2D surfaceData, inout InputData2D inputData, inout half4 debugColor)
{
    if (CalculateDebugColorMaterialSettings(surfaceData, inputData, debugColor))
    {
        return _DebugMaterialMode != DEBUGMATERIALMODE_SPRITE_MASK;
    }
    else if (CalculateDebugColorForRenderingSettings(surfaceData, inputData, debugColor))
    {
        return true;
    }
    else if (CalculateDebugColorLightingSettings(surfaceData, inputData, debugColor))
    {
        return true;
    }
    else if (CalculateDebugColorValidationSettings(surfaceData, inputData, debugColor))
    {
        return true;
    }
    else
    {
        return false;
    }
}

#else

#define SETUP_DEBUG_TEXTURE_DATA_2D(inputData, positionWS, texture)
#define SETUP_DEBUG_DATA_2D(inputData, positionWS)

#endif

#endif


// ====================================================================================================================
// \Library\PackageCache\com.unity.render-pipelines.universal@14.0.9\ShaderLibrary\2D\Include\CombinedShapeLightShared.hlsl
// ====================================================================================================================

half _HDREmulationScale;
half _UseSceneLighting;

void ShapeLightAll(in SurfaceData2D surfaceData, in InputData2D inputData,
    in float lightWeight, in float2 lightingUV, in float mask,
    out half4 shapeLight0Modulate, out half4 shapeLight0Additive,
    out half4 shapeLight1Modulate, out half4 shapeLight1Additive,
    out half4 shapeLight2Modulate, out half4 shapeLight2Additive,
    out half4 shapeLight3Modulate, out half4 shapeLight3Additive)
{
    #if USE_SHAPE_LIGHT_TYPE_0
        half4 shapeLight0 = SAMPLE_TEXTURE2D(_ShapeLightTexture0, sampler_ShapeLightTexture0, lightingUV);
        shapeLight0 *= lightWeight;
    
        if (any(_ShapeLightMaskFilter0))
        {
            half4 processedMask = (1 - _ShapeLightInvertedFilter0) * mask + _ShapeLightInvertedFilter0 * (1 - mask);
            shapeLight0 *= dot(processedMask, _ShapeLightMaskFilter0);
        }
    
        shapeLight0Modulate = shapeLight0 * _ShapeLightBlendFactors0.x;
        shapeLight0Additive = shapeLight0 * _ShapeLightBlendFactors0.y;
    #else
        shapeLight0Modulate = 0;
        shapeLight0Additive = 0;
    #endif
    
    #if USE_SHAPE_LIGHT_TYPE_1
        half4 shapeLight1 = SAMPLE_TEXTURE2D(_ShapeLightTexture1, sampler_ShapeLightTexture1, lightingUV);
        shapeLight1 *= lightWeight;
    
        if (any(_ShapeLightMaskFilter1))
        {
            half4 processedMask = (1 - _ShapeLightInvertedFilter1) * mask + _ShapeLightInvertedFilter1 * (1 - mask);
            shapeLight1 *= dot(processedMask, _ShapeLightMaskFilter1);
        }
    
        shapeLight1Modulate = shapeLight1 * _ShapeLightBlendFactors1.x;
        shapeLight1Additive = shapeLight1 * _ShapeLightBlendFactors1.y;
    #else
        shapeLight1Modulate = 0;
        shapeLight1Additive = 0;
    #endif
    
    #if USE_SHAPE_LIGHT_TYPE_2
        half4 shapeLight2 = SAMPLE_TEXTURE2D(_ShapeLightTexture2, sampler_ShapeLightTexture2, lightingUV);
        shapeLight2 *= lightWeight;
    
        if (any(_ShapeLightMaskFilter2))
        {
            half4 processedMask = (1 - _ShapeLightInvertedFilter2) * mask + _ShapeLightInvertedFilter2 * (1 - mask);
            shapeLight2 *= dot(processedMask, _ShapeLightMaskFilter2);
        }
    
        shapeLight2Modulate = shapeLight2 * _ShapeLightBlendFactors2.x;
        shapeLight2Additive = shapeLight2 * _ShapeLightBlendFactors2.y;
    #else
        shapeLight2Modulate = 0;
        shapeLight2Additive = 0;
    #endif
    
    #if USE_SHAPE_LIGHT_TYPE_3
        half4 shapeLight3 = SAMPLE_TEXTURE2D(_ShapeLightTexture3, sampler_ShapeLightTexture3, lightingUV);
        shapeLight3 *= lightWeight;
    
        if (any(_ShapeLightMaskFilter3))
        {
            half4 processedMask = (1 - _ShapeLightInvertedFilter3) * mask + _ShapeLightInvertedFilter3 * (1 - mask);
            shapeLight3 *= dot(processedMask, _ShapeLightMaskFilter3);
        }
    
        shapeLight3Modulate = shapeLight3 * _ShapeLightBlendFactors3.x;
        shapeLight3Additive = shapeLight3 * _ShapeLightBlendFactors3.y;
    #else
        shapeLight3Modulate = 0;
        shapeLight3Additive = 0;
    #endif
    
}

half4 CombinedShapeLightShared(in SurfaceData2D surfaceData, in InputData2D inputData, in float lightWeight)
{
    #if defined(DEBUG_DISPLAY)
    half4 debugColor = 0;

    if (CanDebugOverrideOutputColor(surfaceData, inputData, debugColor))
    {
        return debugColor;
    }
    #endif

    half alpha = surfaceData.alpha;
    half4 color = half4(surfaceData.albedo, alpha);
    const half4 mask = surfaceData.mask;
    const half2 lightingUV = inputData.lightingUV;

    if (alpha == 0.0) discard;

    half4 finalOutput;
#if !USE_SHAPE_LIGHT_TYPE_0 && !USE_SHAPE_LIGHT_TYPE_1 && !USE_SHAPE_LIGHT_TYPE_2 && ! USE_SHAPE_LIGHT_TYPE_3
    finalOutput = color;
#else
    half4 shapeLight0Modulate, shapeLight0Additive;
    half4 shapeLight1Modulate, shapeLight1Additive;
    half4 shapeLight2Modulate, shapeLight2Additive;
    half4 shapeLight3Modulate, shapeLight3Additive;
    ShapeLightAll(surfaceData, inputData, lightWeight, lightingUV, mask,
        shapeLight0Modulate, shapeLight0Additive,
        shapeLight1Modulate, shapeLight1Additive,
        shapeLight2Modulate, shapeLight2Additive,
        shapeLight3Modulate, shapeLight3Additive);
    half4 finalModulate = shapeLight0Modulate + shapeLight1Modulate + shapeLight2Modulate + shapeLight3Modulate;
    half4 finalAdditve = shapeLight0Additive + shapeLight1Additive + shapeLight2Additive + shapeLight3Additive;
    color = color * finalModulate + finalAdditve;
    finalOutput = _HDREmulationScale * color;
#endif

       
    finalOutput.a = alpha;

    return max(0, finalOutput);
}
#endif



// ================================================================================================
// ================================================================================================
// ================================================================================================

